<?php
/**
 * Pipahr
 *
 * @author Wei Ma
 * @copyright 2014 pipahr.com
 *
 * @since   3.3
 */

class PReportsHelper
{
   public static function showNavMenu($activeitem = '') {
      $app = PFactory::getApplication();
      $fid = JRequest::getInt('fid', 0);
      $fid = empty($fid) ? $app->getUserState('fid_reports', 0) : $fid;
      $masteruser = Pfactory::isMasterUser();
      if (!$masteruser)
         return null;
      $items = array(
         'summary'  => array('name' => '综合统计', 'href' => OC_Helper::linkToRoute('reports_home'), 'list' => array('summary')),
         'position' => array('name' => '职位统计', 'href' => OC_Helper::linkToRoute('reports_position'), 'list' => array('position', 'positiondetail')),
         'contrast' => array('name' => '对比统计', 'href' => OC_Helper::linkToRoute('reports_contrast'), 'list' => array('contrast')),
         'subaccount' => array('name' => '子账号统计', 'href' => OC_Helper::linkToRoute('reports_subaccount'), 'list' => array('subaccount', 'subaccountdetail'))
      );
      echo '<ul class="clearfix">';
      foreach ($items as $key => $item) {
         $selected = in_array($activeitem, $item['list']);
         $href = $item['href'];
         if (!empty($fid) && $key != 'subaccount') {
            $href = PResumesHelper::getSearchItemHref($href, 'fid', $fid)[0];
         }
         echo '<li ' . ($selected ? 'class="cur_li"' : '') . '><a href="' . $href . '">' . $item['name'] . '</a></li>';
      }
      echo '</ul>';
   }

   public static function showDateBar($contrast = false) {
      $start = JRequest::getString('start', '');
      $end = JRequest::getString('end', '');
      $datetype = JRequest::getString('datetype', '');

      echo '<div class="datebar rep_timearea clearfix">
               <ul class="datebtnsbox tabtimeselect pull-left">
                  <li class="cur_li"><a href="javascript:;" class="timeselect month" data-datetype="month">月</a></li>
                  <li><a href="javascript:;" class="timeselect quarter" data-datetype="quarter">季度</a></li>
                  <li><a href="javascript:;" class="timeselect year" data-datetype="year">年</a></li>
                  <li><a href="javascript:;" class="timeselect custom" data-datetype="custom">自定义</a></li>
               </ul>
               
               <div class="timeselect pull-left"> ' .
                  '<span class="timeselectbtn arrowbox" style="' . ($contrast ? 'display:none' : '') . '" data-target=".startxt"><i class="arrowlt"></i></span>&nbsp;'.
                  (!$contrast ? '<input class="dateinput startxt" data-target=".start" data-arrow="true" data-year="" data-month="" data-quarter="" type="button" value="" readonly="true">
                                <span class="endtxtbox" style="display:none;"> 至 <input class="dateinput endtxt" data-target=".end" type="button" value="" readonly="true"></span>' 
                                :
                                '<input class="dateinput startxt cns" name="cnsfrom" data-target=".start" data-arrow="true" data-year="" data-month="" data-quarter="" type="button" value="" readonly="true">
                                 <span class="endtxtbox"> 对比 <input class="dateinput endtxt cns" name="cnsto" data-target=".end" type="button" value="" readonly="true"></span>') . '
                  <a href="javascript:;" class="ppp_whitebtn startcollect">开始统计</a>
                  <span class="timeselectbtn arrowbox" style="' . ($contrast ? 'display:none' : '') . '" data-target=".startxt"><i class="arrowrt blackgrey"></i></span>
                  <input class="start" name="start" type="hidden" value="' . $start . '">
                  <input class="end" name="end" type="hidden" value="' . $end . '">' .
                  ($contrast ? '<input class="tostart" name="tostart" type="hidden" value=""><input class="toend" name="toend" type="hidden" value="">' : '') .
                  '<input class="datetype" name="datetype" type="hidden" value="' . $datetype . '">
               </div>
               <em class="pull-right">小贴士：所有统计数据为时间段内新增数据</em>
            </div>';
   }

   public static function hasReport() {
      $db = PFactory::getDbo();
      $query = "SELECT COUNT(*) FROM jos_jobs_report WHERE user_id=" . Pfactory::getMasterUser()->id;
      $db->setQuery($query);
      return $db->loadResult();
   }

   // build search url, which carry original url params
   public static function getUrl($params = array('path','query'), $url = '') {
      $JURI = empty($url) ? JURI::getInstance() : JURI::getInstance($url);
      return $JURI->toString($params);
   }

   public static function updateJobgroup($fid = 0, $uid = '', $type = 'reports') {
      $fid = empty($fid) ? JRequest::getInt('fid', 0) : $fid;
      $uid = empty($uid) ? OC_User::getUser() : $uid;

      $data = array();
      if(!empty($fid)) {
         $data['fid'] = $fid;
      }

      $db = PFactory::getDbo();
      $query = "UPDATE jos_jobs_report SET data=" . $db->quote(json_encode($data)) . " WHERE user_id=" . $db->quote($uid);
      $db->setQuery($query);
      if (!$db->execute())
         return false;

      $app = PFactory::getApplication();
      $app->setUserState('fid_'.$type, $fid);

      return true;
   }

   public static function getLastJobgroup($uid = '') {
      $uid = empty($uid) ? OC_User::getUser() : $uid;

      $db = PFactory::getDbo();
      $query = "SELECT data FROM jos_jobs_report WHERE user_id=" . $db->quote($uid);
      $db->setQuery($query);
      return json_decode($db->loadResult(), true);
   }

   public static function getJobDropTitle($fid = 0, $uid = '', $type = 'reports') {
      $curcityList = array();
      $curcityids = array();
      $uid = empty($uid) ? OC_User::getUser() : $uid;
      if (empty($fid)) {
         $title = '全部职位';
      } else {
         $dirInfo = PJobpositionHelper::getDirInfo($fid, $uid);
         if (!empty($dirInfo)) {
            if ($dirInfo['type'] == 'user')
               $title = '文件夹-' . $dirInfo['foldername'];
            elseif ($dirInfo['foldername'] == 'reports_internal') {
               $folderData = json_decode($dirInfo['data'], true);
               if (!empty($folderData['cityids'])) {
                  $title = '地区-';
                  $curcityList = PResumesHelper::getJobCityList($uid, false, '', $folderData['cityids']);
                  if (!empty($curcityList)) {
                     $cText = array();
                     $curcityids = array_keys($curcityList);
                     foreach ($curcityList as $c) {
                        $cText[] = (ProvinceHelper::getMuniProvinceId($c['cityid']) ? $c['province'] : $c['province'] . $c['city']);
                     }
                     $title .= implode(',', $cText);
                  }
               } else {
                  return false;
               }
            } elseif ($dirInfo['type'] == $type)
               $title = ('自定义-' . $dirInfo['foldername']);
         } else {
            return false;
         }
      }
      return array($title, $curcityids);
   }
}

